package com.piece.module.file.util.pdf.event;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import com.piece.module.file.util.pdf.BuilderContext;
import com.piece.module.file.util.pdf.PdfColor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class HeaderIEvent implements IEventHandler {

    private BuilderContext builderContext;
    protected PdfFont font;

    private float width = 45;
    private float height = 45;

    public HeaderIEvent(PdfFont font, BuilderContext builderContext) {
        this.font = font;
        this.builderContext = builderContext;
    }

    @Override
    public void handleEvent(Event event) {
        if (builderContext.isShowHeader()) {
            try {
                PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
                PdfDocument pdfDocument = docEvent.getDocument();
                PdfPage page = pdfDocument.getPage(pdfDocument.getNumberOfPages());
                Rectangle pageSize = page.getPageSize();
                PdfCanvas pdfCanvas = new PdfCanvas(page);

                float left = 50;
                // 画Logo
                if (builderContext.getLogo() != null) {
                    drawHeaderLogo(page, pageSize, left);
                    left += width;
                }
                pdfCanvas.saveState();
                // 画横线
                drawHeaderLine(pdfCanvas, pageSize, 40);
                // 画Text
                if (null != builderContext.getLogoText()) {
                    drawHeaderText(page, pageSize, left + 2);
                }
                pdfCanvas.restoreState();
            } catch (Exception e) {
                log.error("写入Pdf页头出错：{}", e.getMessage());
            }
        }
    }

    private void drawHeaderLogo(PdfPage page, Rectangle pageSize, float left) {
        try {
            Image image = new Image(ImageDataFactory.create(builderContext.getLogo()));
            image.setWidth(width);
            image.setHeight(height);
            Rectangle rectangle = new Rectangle(left, pageSize.getTop() - 50, width, height);
            Canvas canvas = new Canvas(page, rectangle);
            canvas.add(image).flush();
            canvas.close();
        } catch (Exception e) {
            log.error("添加Pdf页眉Logo错误：{}", e.getMessage());
        }
    }

    private void drawHeaderText(PdfPage page, Rectangle pageSize, float left) {
        Rectangle rectangle = new Rectangle(left, pageSize.getTop() - 50, pageSize.getWidth() - left, height);
        Canvas canvas = new Canvas(page, rectangle);
        canvas.setFont(font)
                .setFontSize(11)
                .setFontColor(PdfColor.getThemeColor())
                .showTextAligned(builderContext.getLogoText(), left, pageSize.getTop() - 50, TextAlignment.LEFT, VerticalAlignment.BOTTOM, 0.0F)
                .flush();
        canvas.close();
    }

    private void drawHeaderLine(PdfCanvas pdfCanvas, Rectangle pageSize, float left) {
        pdfCanvas.setStrokeColor(PdfColor.getBlackColor());
        pdfCanvas.setLineWidth(0.5f);
        float y = pageSize.getTop() - 55;
        pdfCanvas.moveTo(left, y).lineTo(pageSize.getWidth() - left, y).stroke();
    }
}
